// 添加子模块
mod memory_store;
#[cfg(feature = "postgres")]
mod postgres_store;
#[cfg(feature = "redis-store")]
mod redis_store;

// 重新导出
pub use self::memory_store::InMemoryEventStore;
#[cfg(feature = "postgres")]
pub use self::postgres_store::PostgresEventStore;
#[cfg(feature = "redis-store")]
pub use self::redis_store::RedisEventStore;

// 外部导入
use std::any::Any;
use std::fmt::{Debug, Display}; 
use async_trait::async_trait;
use thiserror::Error;

/// 持久化错误类型
#[derive(Error, Debug)]
pub enum PersistenceError {
    /// 存储错误
    #[error("Storage error: {0}")]
    Storage(String),
    
    /// 序列化错误
    #[error("Serialization error: {0}")]
    Serialization(String),
    
    /// 反序列化错误
    #[error("Deserialization error: {0}")]
    Deserialization(String),
    
    /// 无法找到事件
    #[error("Events not found for actor {0}")]
    EventsNotFound(String),
    
    /// 无法找到快照
    #[error("Snapshot not found for actor {0}")]
    SnapshotNotFound(String),
    
    /// 未知错误
    #[error("Unknown error: {0}")]
    Unknown(String),
}

/// 持久化事件特征
pub trait PersistentEvent: Debug + Clone + Send + Sync + 'static {}

/// 持久化状态特征
pub trait PersistentState: Debug + Clone + Send + Sync + 'static {}

/// 事件存储接口
#[async_trait]
pub trait EventStore {
    /// 事件类型
    type Event: PersistentEvent;
    /// 状态类型
    type State: PersistentState;
    
    /// 保存事件到存储
    async fn save_event(&self, actor_id: &str, event: &Self::Event, index: u64) -> Result<(), PersistenceError>;
    
    /// 获取事件列表
    async fn get_events(&self, actor_id: &str, start_index: u64) -> Result<Vec<Self::Event>, PersistenceError>;
    
    /// 保存快照
    async fn save_snapshot(&self, actor_id: &str, state: &Self::State) -> Result<(), PersistenceError>;
    
    /// 获取快照
    async fn get_snapshot(&self, actor_id: &str) -> Result<Option<Self::State>, PersistenceError>;
    
    /// 删除事件和快照
    async fn delete_events(&self, actor_id: &str) -> Result<(), PersistenceError>;
}

/// 持久化Actor特征
#[async_trait]
pub trait PersistentActor: Send + Sync {
    /// 事件类型
    type Event: PersistentEvent;
    /// 状态类型
    type State: PersistentState;
    
    /// 持久化ID，用于在存储中唯一标识Actor
    fn persistence_id(&self) -> String;
    
    /// 应用事件更新状态
    fn apply_event(&mut self, event: Self::Event);
    
    /// 从状态恢复
    fn recover_from_snapshot(&mut self, state: Self::State);
    
    /// 获取当前状态
    fn get_state(&self) -> Self::State;
    
    /// 收到恢复完成通知
    async fn recovery_completed(&mut self);
    
    /// 持久化事件
    async fn persist_event(&mut self, event: Self::Event) -> Result<(), PersistenceError>;
    
    /// 保存快照
    async fn save_snapshot(&mut self) -> Result<(), PersistenceError>;
} 